//
//  RegisterViewController.m
//  iOS-CtrlV
//
//  Created by Wang Li on 12-12-19.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "RegisterViewController.h"
#import "stringAddition.h"
#import "ASIFormDataRequest.h"
#import "CJSONDeserializer.h"
#import "AppDelegate.h"
#import "MainTabBarController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController
@synthesize m_Target;
@synthesize m_Target_UID;
- (id)init
{
    if (self = [super init]) {
        // Custom initialization
    }
    return self;
}

-(void) dealloc
{
    [self hudWasHidden:nil];
    [m_Target release];
    [m_Target_UID release];
    [_PasswordInputField resignFirstResponder];
    [_PasswordInputField release];
    [_EmailInputField resignFirstResponder];
    [_EmailInputField release];
    [_NickNameInputField resignFirstResponder];
    [_NickNameInputField release];
    [_RegTableView release];
    [_BackBttBarItem release];
    [_titleBttBarItem release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:193/255.f green:193/255.f blue:193/255.f alpha:1]];
	// Do any additional setup after loading the view.

    _titleBttBarItem = [[MainTabBarController setTitle:@"注册"] retain];
    

    _BackBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    _BackBttBarItem.frame = CGRectMake(5, 7, 50, 30);
    [_BackBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    [_BackBttBarItem setTitle:@"返回" forState:UIControlStateNormal];
    [_BackBttBarItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _BackBttBarItem.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [_BackBttBarItem addTarget:self action:@selector(navBackBtt) forControlEvents:UIControlEventTouchUpInside];
    [_BackBttBarItem retain];
    
    _RegTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-44) style:UITableViewStyleGrouped];
    _RegTableView.delegate = self;
    _RegTableView.dataSource = self;
    [self.view addSubview:_RegTableView];
    [_RegTableView setBackgroundColor:BACKGROUND_COLOR];
    [_RegTableView setBackgroundView:nil];
    
    
    _EmailInputField = [[UITextField alloc] initWithFrame:CGRectMake(54, 12, 250, 30)];
    _PasswordInputField = [[UITextField alloc] initWithFrame:CGRectMake(54, 12, 250, 30)];
    _NickNameInputField = [[UITextField alloc] initWithFrame:CGRectMake(54, 12, 250, 30)];
    
    _PasswordInputField.secureTextEntry =YES;
    
    _EmailInputField.returnKeyType =UIReturnKeyDone;
    _PasswordInputField.returnKeyType =UIReturnKeyDone;
    _NickNameInputField.returnKeyType =UIReturnKeyDone;
    
    _EmailInputField.delegate = self;
    _PasswordInputField.delegate = self;
    _NickNameInputField.delegate = self;
    
    UIView* tBottomView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 302, 40)];
    [tBottomView setBackgroundColor:[UIColor clearColor]];
    UIButton* tRegBtt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    tRegBtt.frame =CGRectMake(8, 5, 302, 40);
    [tRegBtt setBackgroundImage:[UIImage imageNamed:@"button_yellow_l.png"] forState:UIControlStateNormal];
    [tRegBtt.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
    [tRegBtt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tRegBtt setTitle:@"注册" forState:UIControlStateNormal];
    [tRegBtt addTarget:self action:@selector(registerBttPressed) forControlEvents:UIControlEventTouchUpInside];
    [tBottomView addSubview:tRegBtt];
    _RegTableView.tableFooterView = tBottomView;
    [tBottomView release];
}

-(void) viewWillDisappear:(BOOL)animated
{
    [_BackBttBarItem removeFromSuperview];
    [_titleBttBarItem removeFromSuperview];
}

-(void) viewWillAppear:(BOOL)animated
{
    if ([_BackBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:_BackBttBarItem];
    }
    if ([_titleBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:_titleBttBarItem];
    }
}

-(void) navBackBtt
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) registerBttPressed
{
    NSString* tErrorStr = nil;
    UIAlertView* tTipAlert = nil;
    if ([_EmailInputField.text length] == 0||
        [_PasswordInputField.text length] == 0||
        [_NickNameInputField.text length] == 0) {
        tErrorStr = @"相关内容不可为空";
        tTipAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:tErrorStr delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles: nil];
        [tTipAlert show];
        [tTipAlert release];
    }else{
        
        HttpInterfaceManager* iHttpInterface =[HttpInterfaceManager sharedManager];
        iHttpInterface.delegate =self;
        [iHttpInterface RegisterRequestGet:_NickNameInputField.text email:_EmailInputField.text code:_PasswordInputField.text];
        [self addHudLoad];
        
    }
}




#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    _MBHUD.delegate = nil;
	[_MBHUD removeFromSuperview];
	[_MBHUD release];
	_MBHUD=nil;
}

-(void) addHudLoad{
	if (_MBHUD == nil) {
		_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
		[self.navigationController.view addSubview:_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		_MBHUD.delegate = self;
		_MBHUD.labelText = @"请求数据中...";
		[_MBHUD show:YES];
	}
}

#pragma mark-
#pragma UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark-
#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark-
#pragma UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tSStrCell = @"regcell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tSStrCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tSStrCell] autorelease];
    }
    int tRow =[indexPath row];
    for (id tSubObject in tCell.subviews) {
        if ([tSubObject isKindOfClass:[UITextField class]]) {
            [tSubObject removeFromSuperview];
        }
    }
    [tCell.textLabel setFont:[UIFont systemFontOfSize:14]];
    tCell.accessoryType = UITableViewCellAccessoryNone;
    tCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tRow == 0) {
        tCell.textLabel.text =@"邮箱";
        [tCell  addSubview:_EmailInputField];
    }else if(tRow == 1){
        tCell.textLabel.text =@"密码";
        [tCell  addSubview:_PasswordInputField];
    }else if(tRow == 2){
        tCell.textLabel.text =@"昵称";
        [tCell  addSubview:_NickNameInputField];
    }
    return tCell;
}
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsRegister:(NSDictionary*)dic
{
     [self hudWasHidden:nil];
    int tErrorCode= [[dic objectForKey:@"error_code"] intValue];
    if (tErrorCode == 0 ) {
        NSDictionary* dic1 = [dic objectForKey:@"data"];
        NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
        if (dic1 != nil) {
            [tUserDef setObject:_EmailInputField.text forKey:@"huikaoaccount"];
            
            NSString* user_id =[dic1 objectForKey:@"user_id"];
            [tUserDef setObject:user_id forKey:@"user_id"];
            
            NSString* name = [dic1 objectForKey:@"username"];
            [tUserDef setObject:name forKey:@"username"];
            
            NSString* email =[dic1 objectForKey:@"email"];
            [tUserDef setObject:email forKey:@"email"];
            
            NSString* userurl = [dic1 objectForKey:@"avatar"] ;
            [tUserDef setObject:userurl forKey:@"avatar"];
            
            NSString* privateCod = [dic1 objectForKey:@"private_code"] ;
            [tUserDef setObject:privateCod forKey:@"private_code"];
        }
        
        MainTabBarController* tMainTabController = (MainTabBarController*)self.navigationController.tabBarController;
        tMainTabController.m_UserDic = dic;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"error_msg"] isCue:YES delayTime:2.0f];
    }

}
-(void)RequestResultsErrer:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"error_msg"] isCue:YES delayTime:2.0f];
    
}
@end
